package sjd.ahsj.springbootmybatisplusoracle.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sjd.ahsj.springbootmybatisplusoracle.entity.IDataDefine;
import sjd.ahsj.springbootmybatisplusoracle.entity.IFactory;
import sjd.ahsj.springbootmybatisplusoracle.entity.IGroup;
import sjd.ahsj.springbootmybatisplusoracle.interceptor.BaseContextHandler;
import sjd.ahsj.springbootmybatisplusoracle.interceptor.IgnoreAuth;
import sjd.ahsj.springbootmybatisplusoracle.model.ReturnObject;
import sjd.ahsj.springbootmybatisplusoracle.service.IIDataDefineService;
import sjd.ahsj.springbootmybatisplusoracle.service.IIFactoryService;
import sjd.ahsj.springbootmybatisplusoracle.service.IIGroupService;
import sjd.ahsj.springbootmybatisplusoracle.service.IIUserService;
import sjd.ahsj.springbootmybatisplusoracle.util.DateUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电站 前端控制器
 * </p>
 *
 * @author zhanghaogao
 * @since 2018-10-18
 */

@Api(description = "电站")
@RestController
@RequestMapping("/iFactory")
public class IFactoryController {
    @Autowired
    private IIFactoryService iiFactoryService;
    @Autowired
    private IIDataDefineService iiDataDefineService;
    @Autowired
    private IIUserService iiUserService;
    @Autowired
    private IIGroupService iiGroupService;

    @IgnoreAuth
    @ApiOperation("生成电站实时和历史表")
    @RequestMapping(value = "/createCurrentAndHistory", method = RequestMethod.POST)
    public ReturnObject createCurrentAndHistory(@ApiParam("电站id") @RequestParam Long factoryId) {
        List<IDataDefine> list = iiDataDefineService.selectList(new EntityWrapper<IDataDefine>().eq("factory_id",
                factoryId));
        iiFactoryService.createCurrentAndHistory(factoryId, list);
        return new ReturnObject(0, "");
    }


    @ApiOperation("获取用户可查看的电站")
    @RequestMapping(value = "/getUserFactory", method = RequestMethod.POST)
    public ReturnObject<List<IFactory>> getUserFactory() {
        return new ReturnObject<>(0, "获取成功",
                iiFactoryService.selectBatchIds(Arrays.asList(iiUserService.selectById(BaseContextHandler.getUserID()).getFactoryId().split(","))));
    }


    @ApiOperation("获取所有电站")
    @RequestMapping(value = "/getAllFactory", method = RequestMethod.POST)
    public ReturnObject<List<IFactory>> getAllFactory() {
        return new ReturnObject<>(0, "获取成功",
                iiFactoryService.selectList(new EntityWrapper<>()));
    }

    @ApiOperation("获取电站下的机组")
    @RequestMapping(value = "/getGroup", method = RequestMethod.POST)
    public ReturnObject<List<IGroup>> getGroup(@ApiParam("电站id") @RequestParam Integer factoryId) {
        List<IGroup> list = iiGroupService.selectList(new EntityWrapper<IGroup>().eq("factory_id", factoryId));
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = iiFactoryService.selectFactoryInfo(i + 2, factoryId);
            if (i + 2 < 10) {
                System.out.println("GA_" + factoryId + "0" + (i + 2) + "1100");
                list.get(i).setStatus1(map.get("GA_" + factoryId + "0" + (i + 2) + "1100") != null ?
                        map.get("GA_" + factoryId + "0" + (i + 2) + "1100").toString() : null);
                list.get(i).setStatus2(map.get("GA_" + factoryId + "0" + (i + 2) + "1500") != null ?
                        map.get("GA_" + factoryId + "0" + (i + 2) + "1500").toString() : null);
            } else {
                list.get(i).setStatus1(map.get("GA_" + factoryId + (i + 2) + "1100") != null ?
                        map.get("GA_" + factoryId + (i + 2) + "1100").toString() : null);
                list.get(i).setStatus2(map.get("GA_" + factoryId + (i + 2) + "1500") != null ?
                        map.get("GA_" + factoryId + (i + 2) + "1100").toString() : null);
            }

        }
        return new ReturnObject<>(0, "获取成功", list);
    }


    @ApiOperation("新增电站")
    @RequestMapping(value = "/saveFactory", method = RequestMethod.POST)
    public ReturnObject<List<IFactory>> saveFactory(@ApiParam("名称") @RequestParam String name,
                                                    @ApiParam("英文名称") @RequestParam String enName,
                                                    @ApiParam("图片地址") @RequestParam String address) {
        IFactory factory = new IFactory();
        factory.setName(name);
        factory.setEnName(enName);
        factory.setAddress(address);
        iiFactoryService.insert(factory);
        return new ReturnObject<>(0, "新增成功");
    }

    @ApiOperation("编辑电站")
    @RequestMapping(value = "/editFactory", method = RequestMethod.POST)
    public ReturnObject<List<IFactory>> editFactory(@ApiParam("id") @RequestParam Integer id,
                                                    @ApiParam("名称") @RequestParam String name,
                                                    @ApiParam("英文名称") @RequestParam String enName,
                                                    @ApiParam("图片地址") @RequestParam String address) {
        IFactory factory = iiFactoryService.selectById(id);
        factory.setName(name);
        factory.setEnName(enName);
        factory.setAddress(address);
        iiFactoryService.updateById(factory);
        return new ReturnObject<>(0, "编辑成功");
    }


    @ApiOperation("删除电站")
    @RequestMapping(value = "/delFactory", method = RequestMethod.POST)
    public ReturnObject<List<IFactory>> delFactory(@ApiParam("id") @RequestParam Integer id) {
        iiFactoryService.deleteById(id);
        return new ReturnObject<>(0, "删除成功");
    }

    @ApiOperation("新增机组")
    @RequestMapping(value = "/saveGroup", method = RequestMethod.POST)
    public ReturnObject saveGroup(@ApiParam("名称") @RequestParam String name,
                                  @ApiParam("英文名称") @RequestParam String enName,
                                  @ApiParam("电站id") @RequestParam Integer factoryId) {
        IGroup iGroup = new IGroup();
        iGroup.setName(name);
        iGroup.setFactoryId(factoryId);
        iGroup.setEnName(enName);
        iiGroupService.insert(iGroup);
        return new ReturnObject<>(0, "新增成功");
    }

    @ApiOperation("编辑机组")
    @RequestMapping(value = "/editGroup", method = RequestMethod.POST)
    public ReturnObject editGroup(@ApiParam("id") @RequestParam Long id,
                                  @ApiParam("名称") @RequestParam String name,
                                  @ApiParam("英文名称") @RequestParam String enName,
                                  @ApiParam("电站id") @RequestParam Integer factoryId) {
        IGroup iGroup = iiGroupService.selectById(id);
        iGroup.setName(name);
        iGroup.setEnName(enName);
        iGroup.setFactoryId(factoryId);
        iiGroupService.updateById(iGroup);
        return new ReturnObject<>(0, "编辑成功");
    }


    @ApiOperation("删除机组")
    @RequestMapping(value = "/delGroup", method = RequestMethod.POST)
    public ReturnObject delGroup(@ApiParam("id") @RequestParam Integer id) {
        iiGroupService.deleteById(id);
        return new ReturnObject<>(0, "删除成功");
    }

    @ApiOperation("获取机组基础信息")
    @RequestMapping(value = "/selectFactoryInfo", method = RequestMethod.POST)
    public ReturnObject<Map<String, Object>> selectFactoryInfo(@ApiParam("电站id") @RequestParam Integer factoryId,
                                                               @ApiParam("第几个主机（不是主机id）") @RequestParam Integer num) {
        return new ReturnObject<>(0, "获取成功", iiFactoryService.selectFactoryInfo(num, factoryId));
    }


    @ApiOperation("获取电站历史信息")
    @RequestMapping(value = "/selectFactoryInfoHistory", method = RequestMethod.POST)
    public ReturnObject<List<Map<String, Object>>> selectFactoryInfoHistory(@ApiParam("电站id") @RequestParam Integer factoryId,
                                                                            @ApiParam("日期") @DateTimeFormat(pattern =
                                                                                    "yyyy-MM-dd") @RequestParam Date date) {
        return new ReturnObject<>(0, "获取成功", iiFactoryService.selectFactoryInfoHistory(factoryId, date));
    }


    @ApiOperation("获取绘图信息")
    @RequestMapping(value = "/selectFactoryInfoPlot", method = RequestMethod.POST)
    public ReturnObject<List<Map<String, Object>>> selectFactoryInfoPlot(@ApiParam("电站id") @RequestParam Integer factoryId,
                                                                         @ApiParam("类型（1：秒，2：小时，3：天，4：月）") @RequestParam Integer type) {

        return new ReturnObject<>(0, "获取成功", iiFactoryService.selectPlotInfoBySql(factoryId, type));
    }


}

